#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

// https://leetcode.cn/problems/smallest-range-ii/description/

class Solution
{
public:
    int smallestRangeII(vector<int> &nums, int k)
    {
        sort(nums.begin(), nums.end());
        int ans = nums.back() - nums[0];
        for (int i = 1; i < nums.size(); i++)
        {
            int mx = max(nums[i - 1] + k, nums.back() - k);
            int mn = min(nums[0] + k, nums[i] - k);
            ans = min(ans, mx - mn);
        }
        return ans;
    }
};

int main()
{

    // system("pause");
    return 0;
}
